load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
    name = "scanner",
    srcs = [
        "plpgsql_scan.go",
        "scan.go",
    ],
    importpath = "github.com/cockroachdb/cockroach/pkg/sql/scanner",
    visibility = ["//visibility:public"],
    deps = [
        "//pkg/sql/lexbase",
        "//pkg/sql/plpgsql/parser/lexbase",
    ],
)

go_test(
    name = "scanner_test",
    srcs = [
        "scan_test.go",
        ":gen-token-names",  # keep
    ],
    data = glob(["testdata/**"]),
    embed = [":scanner"],
    deps = [
        "//pkg/sql/lexbase",
        "@com_github_cockroachdb_datadriven//:datadriven",
        "@com_github_stretchr_testify//require",
    ],
)

genrule(
    name = "gen-token-names",
    outs = ["token_names_test.go"],
    cmd = """
    (echo "// Code generated by make. DO NOT EDIT."; \
     echo "// GENERATED FILE DO NOT EDIT"; \
     echo; \
     echo "package scanner"; \
     echo; \
     echo "var tokenNames = map[int]string{"; \
     grep '^const [A-Z][_A-Z0-9]* ' $(location //pkg/sql/parser:sql-goyacc) |\
     awk '{printf("%d: \\"%s\\",\\n", $$4, $$2)}'; \
     echo "}" )> $@
    """,
    tools = [
        "//pkg/sql/parser:sql-goyacc",
    ],
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)
